package co.edu.unal.scrum.client.projects;

import java.util.Date;

import co.edu.unal.scrum.client.WindowViewWithUiHandlers;
import co.edu.unal.scrum.client.resources.Messages;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class ProjectEditorView extends WindowViewWithUiHandlers<ProjectEditorUiHandlers> implements
		ProjectEditorPresenter.MyView {

	

	private Window window;
	private FormPanel form;
	
	private Button saveButton;
	private Button cancelButton;
	
	private FormButtonBinding binding;
	
	private TextField<String> nameField;
	private DateField dateStartField;
	private DateField dateEndField;
	private TextArea descriptionField;
	
	@Inject
	public ProjectEditorView(EventBus eventBus, Messages messages) {
		super(eventBus);
		window = new Window();
		window.setId(ProjectEditorView.class.getName());
		window.setHeading("Project Editor");
		window.setLayout(new FitLayout());
		window.setWidth(350);
		window.setHeight(220);
		window.setModal(true);
		
		form = new FormPanel();
		form.setHeaderVisible(false);
		form.setBodyBorder(false);
		form.setBorders(false);
		form.setButtonAlign(HorizontalAlignment.CENTER);
		
		nameField = new TextField<String>();  
		nameField.setFieldLabel(messages.name());  
		nameField.setAllowBlank(false);
		nameField.setMinLength(4);
		
		dateStartField = new DateField();  
		dateStartField.setFieldLabel(messages.startDate());
		dateStartField.setAllowBlank(false);
		
		dateEndField = new DateField();  
		dateEndField.setFieldLabel(messages.endDate());
		dateEndField.setAllowBlank(false);
		
		descriptionField = new TextArea();  
	    descriptionField.setPreventScrollbars(true);  
	    descriptionField.setFieldLabel(messages.description());
		
	    saveButton = new Button("Submit");  
	    cancelButton = new Button("Cancel");  
	    
		form.add(nameField);
		form.add(dateStartField);
	    form.add(dateEndField);
	    form.add(descriptionField);
	    form.addButton(saveButton);  
	    form.addButton(cancelButton);  
	    
	    binding = new FormButtonBinding(form);  
	    binding.addButton(saveButton);
	    
		window.add(form);
		
		bindCustomUserUiHandlers();
	}

	@Override
	public Widget asWidget() {
		return window;
	}

	protected void bindCustomUserUiHandlers() {
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (getUiHandlers() != null) {
					getUiHandlers().onSaveButtonClicked();
				}
			}
		});
		
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (getUiHandlers() != null) {
					getUiHandlers().onCancelButtonClicked();
				}
			}
		});
	}

	public void setName(String name) {
		nameField.setValue(name);
	}

	public void setStartDate(Date startDate) {
		dateStartField.setValue(startDate);
	}

	public void setEndDate(Date endDate) {
		dateEndField.setValue(endDate);
	}

	public void setDescription(String description) {
		descriptionField.setValue(description);
		
	}

	public String getName() {
		return nameField.getValue();
	}

	public Date getStartDate() {
		return dateStartField.getValue();
	}

	public Date getEndDate() {
		return dateEndField.getValue();
	}

	public String getDescription() {
		return descriptionField.getValue();
	}

}
